<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="8">
        <!-- 用户信息 -->
        <el-card shadow="hover" class="box-card">
          <div class="top-user">
            <div class="img">
              <img :src="img" alt="">
            </div>
            <div class="top-test">{{ name }}</div>
          </div>
          <div class="user-info">
            <div class="time" style="display: flex;">
              <div style="color: #ccc;">本次登陆时间：</div>
              <div style="margin-left: 60px;">{{ time }}</div>
            </div>
            <div class="address" style="display: flex;">
              <div style="color: #ccc;">本次登陆地点：</div>
              <div style="margin-left: 60px;">西安</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <!-- 天气 -->
      <el-col :span="16">
        <el-card shadow="hover" style="height: 250px;">
          <div>
            <div style="font-size: 30px;display: flex;">{{ weather.ymd }}
              <div>
                &nbsp;&nbsp;{{ weather.week }}
                &nbsp;{{ weather.type }}
              </div>
              <div style="margin-left: 20vw;font-size: 26px;">
                {{ weather.notice }}
              </div>
            </div>
            <div style="font-size: 36px;margin: 40px 0 40px 30px;">
              {{ weather.low }} -
              {{ weather.high }}
            </div>
            <div style="display: flex;justify-content: space-around;text-align: center;">
              <div>
                <div style="color: #aaa;">日出</div>
                <div style="margin-top: 3px;font-size: 20px;">
                  {{ weather.sunrise }}
                </div>
              </div>
              <div>
                <div style="color: #aaa;">日落</div>
                <div style="margin-top: 3px;font-size: 20px;">{{ weather.sunset }}</div>
              </div>
              <div>
                <div style="color: #aaa;">风向</div>
                <div style="margin-top: 3px;font-size: 20px;">{{ weather.fx }}{{ weather.fl }}</div>
              </div>
              <div>
                <div style="color: #aaa;">空气质量</div>
                <div style="margin-top: 3px;font-size: 20px;">{{ weather.aqi }}</div>
              </div>
            </div>
          </div>

        </el-card>
      </el-col>
    </el-row>
    <!-- 销量列表 -->
    <el-row :gutter="20" style="margin-top: 20px;">
      <el-col :span="8">
        <el-card shadow="hover">
          <el-table height="54vh" :data="tableData" stripe>
            <el-table-column prop="name" label="农产品" />
            <el-table-column prop="salesVolume" label="销量" />
            <el-table-column prop="inventory" label="库存" />
            <el-table-column prop="price" label="价格" />
          </el-table>
        </el-card>
      </el-col>
      <el-col :span="16" style="margin-bottom: 18px;">
        <!-- 销量条形图 -->
        <el-card shadow="hover" style="height: 230px;">
          <div id="user" style="height: 230px; width: 1200px;"></div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <!-- 销量柱形图 -->
        <el-card shadow="hover" style="width: 550px;height: 300px;" class="box-card">
          <div id="main" style="height: 300px; width: 550px;"></div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <!-- 销量饼图 -->
        <el-card shadow="hover" style="width: 550px;height: 300px;" class="box-card">
          <div id="pie" style="height: 280px; width: 550px;"></div>
        </el-card>
      </el-col>

    </el-row>
  </div>
</template>

<script>
import { getGoodsAll, getTime, getWeather } from "../api/api.js"
export default {
  data() {
    return {
      data: '',
      img: '',
      name: '',
      time: '',
      tableData: [],
      weather: {},
    }
  },
  mounted() {
    // 获取天气
    getWeather().then((res) => {
      console.log(res.data.data[0].weather[0]);
      this.weather = res.data.data[0].weather[0]
    })
    // 获取北京时间
    getTime().then((res) => {
      this.time = res.data.data[0].time
    })
    this.img = localStorage.getItem('image')
    this.name = localStorage.getItem('name')
    this.getList()
    setTimeout(() => {
      this.initCharts()
      this.user()
      this.pie()
    }, 500);
  },
  methods: {
    getList() {
      getGoodsAll().then((res) => {
        console.log(res);
        this.tableData = res.data
        this.data = res.data.splice(0, 14)
      })
    },
    // 各品牌手机笑料统计
    initCharts() {
      // 基于准备好的dom，初始化echarts实例
      var myChart = this.$echarts.init(document.getElementById("main"));
      // 指定图表的配置项和数据
      var option = {
        // 提示框
        tooltip: {
          triggerOn: "mousemove",
        },
        title: {
          text: "农产品销量统计"
        },
        xAxis: {
          data: this.data.map(d => d.name)
        },
        yAxis: {},
        series: [
          {
            name: "销量",
            type: 'bar',
            data: this.data.map(d => d.salesVolume),
            itemStyle: {
              borderRadius: 4
            }
          }
        ]
      };
      myChart.setOption(option);
    },
    // 各品牌手机销量年趋势
    user() {
      var myChart = this.$echarts.init(document.getElementById("user"));
      var option = {
        // 提示框
        tooltip: {
          triggerOn: "mousemove",
        },
        title: {
          text: "农产品销量年趋势"
        },
        xAxis: {
          data: this.data.map(d => d.name)
        },
        yAxis: {},
        series: [
          {
            name: "2017",
            data: this.data.map(d => d.salesVolume),
            type: 'line',
            stack: 'x'
          },
          {
            name: "2018",
            data: [5343, 2131, 4353, 3453, 3454, 5534, 3453, 5435, 4326, 3211, 8363, 3457, 2134, 1321, 6453],
            type: 'line',
            stack: 'x'
          },
          {
            name: "2019",
            data: [5466, 1231, 7686, 7686, 8768, 5466, 6787, 8797, 3213, 9876, 6789, 8769, 9768, 8765, 5424],
            type: 'line',
            stack: 'x'
          },
          {
            name: "2020",
            data: [5466, 8665, 5673, 8341, 4542, 4353, 2667, 4356, 7748, 1345, 6343, 1234, 5463, 5435, 6534],
            type: 'line',
            stack: 'x'
          }
        ]
      };
      myChart.setOption(option);
    },
    // 各品牌手机销量占比
    pie() {
      var myChart = this.$echarts.init(document.getElementById("pie"));
      var option = {
        tooltip: {
          triggerOn: "mousemove",
        },
        title: {
          text: "农产品销量占比"
        },
        series: [
          {
            type: 'pie',
            data: [
              {
                value: 20,
                name: '水稻'
              },
              {
                value: 6,
                name: '鸡蛋'
              },
              {
                value: 5,
                name: '白菜'
              },
              {
                value: 15,
                name: '番茄'
              },
              {
                value: 12,
                name: '小米'
              },
              {
                value: 23,
                name: '猪肉'
              },
              {
                value: 13,
                name: '苹果'
              },
              {
                value: 6,
                name: '红薯'
              },
            ]
          }
        ]
      };
      myChart.setOption(option);
    }
  },
}
</script>

<style>
.top-user {
  display: flex;
}

.img {
  width: 120px;
  height: 120px;
  border-radius: 999999px;
  overflow: hidden;
}

img {
  display: flex;
  width: 100%;
  height: 100%;
}

.top-test {
  line-height: 80px;
  margin-left: 60px;
  font-size: 30px;
  font-weight: 700;
}

.user-info {
  margin-top: 30px;
}

.time {
  margin: 16px 0;
}
</style>